
#-----------------------------------------------------------------------
class Block(object):
	def __init__(self, x, y, w, h ):
		self.x = x
		self.y = y
		self.w = w
		self.h = h
		
		self.neighbours = []	#used by room generator
		
	def moveto(self,x,y):
		self.x = x
		self.y = y
	
	def size(self):
		return int(self.w * self.h)
		
	#set viewport center on specified location	
	def locate(self,x,y):
		
		self.x = x-(self.w/2)
		self.y = y-(self.h/2)
		
	def merge(self,block):
		
		i = max(self.x+self.w, block.x+block.w)
		j = max(self.y+self.h, block.y+block.h)
		
		self.x = min(self.x,block.x)
		self.y = min(self.y,block.y)
		 
		self.w = i-self.x
		self.h = j-self.y
	
	def draw(self, char="X"):
		print "drawing region ["+str(self.x)+","+str(self.y)+","+str(self.x+self.w)+","+str(self.y+self.h)+"]"
		
		for i in range(self.h+1):
			libtcod.console_put_char(self.con, self.x, 	self.y+i, char, libtcod.red)
			libtcod.console_put_char(self.con, self.x+self.w, self.y+i, char, libtcod.red)
			
		for j in range(self.w+1):
			libtcod.console_put_char(self.con, self.x+j, 	self.y, char, libtcod.red)
			libtcod.console_put_char(self.con, self.x+j, 	self.y+self.h, char, libtcod.red)
			
	def scale(self, dx, dy):
		if (self.w + dx*2) > 0:
			self.w = self.w+dx*2
			self.x = self.x-dx
			
		if (self.h+ dy*2) > 0:
			self.h = self.h+dy*2
			self.y = self.y-dy
			
	def get_outer_wall(self,building_block):

		tiles = [[],[],[],[]]
		for i in range(self.w+1):
			for j in range(self.h+1):
				x = self.x+i
				y = self.y+j
				
				if x == building_block.x+building_block.w:
					tiles[0].append((x,y))
				if x == building_block.x:
					tiles[1].append((x,y))
						#~ 
				if y == building_block.y+building_block.h:
					tiles[2].append((x,y))
				if y == building_block.y:
					tiles[3].append((x,y))
		return tiles	
			
	#-------------------------------------------------------------------
	#	Door/Window generation helpers
	#-------------------------------------------------------------------
	def intersect(self,other):
		return (self.x <= other.x+other.w and self.x+self.w >= other.x and
			self.y <= other.y+self.h and self.y+self.h >= other.y)
			
	def connected(self,other):
		for nb in self.neighbours:
			if nb is other:
				return True
		return False
                
	def get_intersection(self,other):
		block = Block(
			max(self.x, other.x),
			max(self.y, other.y),
			min(self.x+self.w, other.x+other.w)-max(self.x, other.x),
			min(self.y+self.h, other.y+other.h)-max(self.y, other.y)
		);
		
		#some obscure tweaks
		if block.w <= 0 and block.h <= 0:
			return None		#something wtfish
			
		if block.w == 0:
			block.w = 1
			
		if block.h == 0:
			block.h = 1

		return block
		
	def to_range(self):
		tiles = []
		for i in range(self.w):
			for j in range(self.h):
				tile = (self.x+i,self.y+j)
				tiles.append(tile)
		return tiles

